package com.cuit.lotery.controller;

import com.cuit.lotery.entity.Setting;
import com.cuit.lotery.entity.Winners;
import com.cuit.lotery.service.SettingService;
import com.cuit.lotery.service.WinnersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
public class Lottery {

    private Integer minStuNo;
    private Integer maxStuNo;

    @Autowired
    WinnersService winnersService;
    @Autowired
    SettingService settingService;

    public void init(){
        Setting range = settingService.getOne("range");
        String val = range.getSval();
        minStuNo = Integer.parseInt(val.split(",")[0]);
        maxStuNo = Integer.parseInt(val.split(",")[1]);
    }
    /* 首页渲染 */
    // 无数据的时候显示空空如也
    @GetMapping(value = {"/index", "/"})
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        List<Winners> datum = winnersService.getTop10();
        mv.addObject("winners", datum);
        init();
        mv.addObject("minStuNo", minStuNo);
        mv.addObject("maxStuNo", maxStuNo);
        mv.setViewName("index");
        return mv;
    }

    /* winner列表局部刷新 */
    @GetMapping("/refreshPart")
    public String aaa(Model model) {
        List<Winners> datum = winnersService.getTop10();
        model.addAttribute("winners", datum);
        return "index::fra_list";
    }

    /* 插入数据 */
    @ResponseBody
    @PostMapping("/insert")
    public void insert(Winners winners){
        winnersService.insert(winners);
    }

}
